package kxfang.com.home.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.orhanobut.logger.Logger;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;

import kxfang.com.base.fragment.MvvmBaseFragment;
import kxfang.com.base.fragment.MvvmLazyFragment;
import kxfang.com.base.utils.GsonUtils;
import kxfang.com.base.viewmodel.IMvvmBaseViewModel;
import kxfang.com.common.adapter.CommonBindingAdapters;
import kxfang.com.common.bean.HomeStoreModel;
import kxfang.com.common.config.HttpConfig;
import kxfang.com.common.views.WrapContentHeightViewPager;
import kxfang.com.home.R;
import kxfang.com.home.databinding.HomeFragmentItemBinding;
import kxfang.com.home.databinding.HomeFragmentStoreItemBinding;
import kxfang.com.home.par.StoreListPackage;
import kxfang.com.http.EasyHttp;
import kxfang.com.http.callback.SimpleCallBack;
import kxfang.com.http.exception.ApiException;

/**
 * Created by ZJ
 * on 2020/5/150
 * Class Content 首页精选商家列表
 */
public class MainOneFragment extends MvvmLazyFragment<HomeFragmentItemBinding, IMvvmBaseViewModel> {

    private BaseQuickAdapter<HomeStoreModel, BaseViewHolder> StoreAdapter;

    public MainOneFragment() {

    }

    public static MainOneFragment newInstance() {
        return new MainOneFragment();
    }

    @Override
    protected void onFragmentFirstVisible() {
        super.onFragmentFirstVisible();
//        setInitView();
        initDate();
    }

    @Override
    public int getLayoutId() {
        return R.layout.home_fragment_item;
    }

    @Override
    public int getBindingVariable() {
        return 0;
    }

    @Override
    protected IMvvmBaseViewModel getViewModel() {
        return null;
    }

    @Override
    protected void onRetryBtnClick() {

    }

    /**
     * 获取数据
     */
    private void initDate() {
        StoreListPackage par = new StoreListPackage();
        par.setIsRecommands(1);
//        par.setRUserID(HawkUtil.getUserId() + "");
        par.setIndustryValue(1);
        EasyHttp.post(HttpConfig.getStoreList)
                .upJson(GsonUtils.toJson(par))
                .cacheKey("Store")
                .execute(new SimpleCallBack<List<HomeStoreModel>>() {
                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(List<HomeStoreModel> homeStoreModels) {
                        Logger.d(homeStoreModels.size()+"---------->");
                        setInitView(homeStoreModels);
                    }

                    @Override
                    public void onCompleted() {

                    }
                });
    }

    private void setInitView(List<HomeStoreModel> list) {
        StoreAdapter = new BaseQuickAdapter<HomeStoreModel, BaseViewHolder>(R.layout.home_fragment_store_item,list) {
            @Override
            protected void onItemViewHolderCreated(@NotNull BaseViewHolder viewHolder, int viewType) {
                DataBindingUtil.bind(viewHolder.itemView);
            }

            @Override
            protected void convert(@NotNull BaseViewHolder baseViewHolder, @Nullable HomeStoreModel homeStoreModel) {
                if (homeStoreModel == null) {
                    return;
                }
                HomeFragmentStoreItemBinding binding = baseViewHolder.getBinding();
                if (binding != null) {
                    binding.setModel(homeStoreModel);
                    binding.executePendingBindings();
                }
            }
        };
        StoreAdapter.setOnItemClickListener((adapter, view, position) -> {
//            Intent intent = new Intent(getContext(), HomeStoreDetailFragment.class);
//            intent.putExtra("id", list.get(position).getRUserID());
//            startActivity(intent);
        });
        viewDataBinding.recycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        viewDataBinding.recycleView.setAdapter(StoreAdapter);
    }
}
